<template>
  <div class="prescription-order-detail">
    <!-- 页面标题 -->
    <div class="page-header">
             <h1>订单详情-{{ getStatusText(orderDetail?.orderInfo?.paymentStatus) }}</h1>
      <button @click="goBack" class="back-button">返回</button>
    </div>



    <div v-if="loading" class="loading">
      <div class="loading-spinner"></div>
      <p>加载中...</p>
    </div>

    <div v-else-if="orderDetail" class="detail-content">
      <!-- 患者信息 -->
      <div class="info-section">
        <h2>患者信息</h2>
        <div class="info-grid">
          <div class="info-item">
            <label>患者姓名：</label>
            <span>{{ orderDetail.orderInfo.patientName || '-' }}</span>
          </div>
          <div class="info-item">
            <label>性别：</label>
            <span>{{ getGenderText(orderDetail.orderInfo.patientGender) }}</span>
          </div>
          <div class="info-item">
            <label>年龄：</label>
            <span>{{ orderDetail.orderInfo.patientAge || '-' }}</span>
          </div>
          <div class="info-item">
            <label>手机号：</label>
            <span>{{ orderDetail.orderInfo.patientPhone || '-' }}</span>
          </div>
          <div class="info-item">
            <label>所患疾病：</label>
            <span>{{ orderDetail.prescriptionInfo.diagnosis || '-' }}</span>
          </div>
          <div class="info-item">
            <label>肝功能：</label>
            <span>{{ orderDetail.orderInfo.liverFunction || '正常' }}</span>
          </div>
          <div class="info-item">
            <label>肾功能：</label>
            <span>{{ orderDetail.orderInfo.kidneyFunction || '正常' }}</span>
          </div>
          <div class="info-item">
            <label>过敏史：</label>
            <span>{{ orderDetail.orderInfo.allergyHistory || '无' }}</span>
          </div>
          <div class="info-item">
            <label>生育计划或状态：</label>
            <span>{{ orderDetail.orderInfo.fertilityPlan || '无' }}</span>
          </div>
        </div>
        
        <div class="info-block">
          <label>病情描述：</label>
          <p>{{ orderDetail.orderInfo.conditionDescription || '暂无描述' }}</p>
        </div>

        <!-- 已选药品及用药情况 -->
        <div class="medicine-section">
          <h3>已选药品及用药情况</h3>
          <div class="medicine-list">
            <div v-for="medicine in orderDetail.medicines" :key="medicine.id" class="medicine-item">
              <div class="medicine-image">
                <img v-if="medicine.imageUrl" :src="medicine.imageUrl" :alt="medicine.name">
                <div v-else class="medicine-placeholder">药品</div>
              </div>
                             <div class="medicine-info">
                 <h4>{{ medicine.name }} <span class="quantity">x{{ medicine.quantity }}</span></h4>
                 <p class="medicine-spec">规格：{{ medicine.specification }}</p>
                 <p class="medicine-price">¥{{ medicine.price }}</p>
                 <p class="medicine-usage">用药情况：{{ medicine.usage }}</p>
                 <button @click="viewOriginalPrescription(medicine.id)" class="view-button">
                   查看原始处方
                 </button>
               </div>
            </div>
          </div>
        </div>
      </div>

      <!-- 处方信息 -->
      <div class="info-section">
        <h2>处方信息</h2>
        <div class="prescription-header">
          <div class="prescription-no">
            <label>NO：</label>
            <span>{{ orderDetail.prescriptionInfo.prescriptionNo }}</span>
          </div>
          <div class="prescription-time">
            <label>开单时间：</label>
            <span>{{ formatDateTime(orderDetail.prescriptionInfo.createdAt) }}</span>
          </div>
          <div class="prescription-status">
            <label>处方单状态：</label>
            <span :class="getStatusClass(orderDetail.prescriptionInfo.status)">
              {{ orderDetail.prescriptionInfo.statusText }}
            </span>
          </div>
          <div class="doctor-info">
            <label>医生：</label>
            <span>{{ orderDetail.orderInfo.doctorName }}</span>
          </div>
          <div class="department-info">
            <label>科室：</label>
            <span>{{ orderDetail.prescriptionInfo.departmentName }}</span>
          </div>
        </div>

        <div class="diagnosis-section">
          <div class="diagnosis-item">
            <label>初步诊断：</label>
            <span>{{ orderDetail.prescriptionInfo.diagnosis || '-' }}</span>
          </div>
          <div class="diagnosis-item">
            <label>处理意见：</label>
            <span>{{ orderDetail.prescriptionInfo.treatmentAdvice || '检测血压,适当运动' }}</span>
          </div>
        </div>

        <!-- 处方详情 -->
        <div class="prescription-details">
          <h3>Rp</h3>
          <div class="prescription-medicines">
                         <div v-for="medicine in orderDetail.medicines" :key="medicine.id" class="prescription-medicine">
               <div class="medicine-header">
                 <h4>【{{ medicine.brand || '药品' }}】{{ medicine.name }}</h4>
                 <span class="quantity">×{{ medicine.quantity }}</span>
               </div>
               <p class="specification">规格：{{ medicine.specification }}</p>
               <p class="dosage">口服：{{ medicine.dosage }}</p>
               <div class="action-buttons">
                 <button @click="viewPrescription(medicine.id)" class="view-button">
                   查看处方
                 </button>
                 <button @click="viewPrescriptionQRCode(medicine.id)" class="view-button">
                   查看处方二维码
                 </button>
               </div>
             </div>
          </div>
        </div>
      </div>

      <!-- 订单信息 -->
      <div class="info-section">
        <h2>订单信息</h2>
        <div class="order-info-grid">
          <div class="info-item">
            <label>订单编号：</label>
            <span>{{ orderDetail.orderInfo.orderNo }}</span>
          </div>
          <div class="info-item">
            <label>状态：</label>
            <span :class="getStatusClass(orderDetail.orderInfo.status)">
              {{ orderDetail.orderInfo.statusText }}
            </span>
          </div>
          <div class="info-item">
            <label>应付金额：</label>
            <span class="amount">¥{{ orderDetail.orderInfo.paymentAmount }}</span>
          </div>
          <div class="info-item">
            <label>优惠券：</label>
            <span class="discount">-¥{{ orderDetail.orderInfo.discountAmount || 0 }}</span>
          </div>
          <div class="info-item">
            <label>实际支付：</label>
            <span class="final-amount">¥{{ getFinalAmount() }}</span>
          </div>
          <div class="info-item">
            <label>创建时间：</label>
            <span>{{ formatDateTime(orderDetail.orderInfo.createdAt) }}</span>
          </div>
        </div>
      </div>
    </div>

    <div v-else class="empty-state">
      <p>未找到订单信息</p>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import api from '../utils/request'


const route = useRoute()
const router = useRouter()

// 响应式数据
const orderDetail = ref<any>(null)
const loading = ref(true)



// 获取订单详情
const loadOrderDetail = async () => {
  try {
    loading.value = true
    const orderId = Number(route.params.id)
    const response = await api.prescriptionOrder.getDetail(orderId)
    
    if (response && response.data) {
      orderDetail.value = response.data
      console.log('订单详情:', orderDetail.value)
    }
  } catch (error) {
    console.error('获取订单详情失败:', error)
    alert('获取订单详情失败，请重试')
  } finally {
    loading.value = false
  }
}

// 返回上一页
const goBack = () => {
  router.back()
}

// 格式化日期时间
const formatDateTime = (dateTime: string | null) => {
  if (!dateTime) return '-'
  return new Date(dateTime).toLocaleString('zh-CN')
}

// 获取状态文本
const getStatusText = (status: number | null) => {
  if (status === null) return '待支付'
  const statusMap: { [key: number]: string } = {
    0: '待支付',
    1: '已完成',
    2: '已失效'
  }
  return statusMap[status] || '待支付'
}

// 获取状态样式类
const getStatusClass = (status: number | null) => {
  if (status === null) return ''
  const statusMap: { [key: number]: string } = {
    0: 'status-pending',
    1: 'status-completed',
    2: 'status-invalid'
  }
  return statusMap[status] || ''
}

// 获取性别文本
const getGenderText = (gender: string | null) => {
  if (!gender) return '-'
  const genderMap: { [key: string]: string } = {
    'male': '男',
    'female': '女'
  }
  return genderMap[gender] || gender
}

// 计算实际支付金额
const getFinalAmount = () => {
  if (!orderDetail.value?.orderInfo) return 0
  const paymentAmount = orderDetail.value.orderInfo.paymentAmount || 0
  const discountAmount = orderDetail.value.orderInfo.discountAmount || 0
  return (paymentAmount - discountAmount).toFixed(2)
}

// 查看原始处方
const viewOriginalPrescription = (medicineId: number) => {
  console.log('查看原始处方，药品ID:', medicineId)
  alert(`查看原始处方 - 药品ID: ${medicineId}\n\n这里会显示原始处方的图片或PDF文档。`)
}

// 查看处方
const viewPrescription = (medicineId: number) => {
  console.log('查看处方，药品ID:', medicineId)
  alert(`查看处方详情 - 药品ID: ${medicineId}\n\n这里会显示处方的详细信息图片。`)
}

// 查看处方二维码
const viewPrescriptionQRCode = (medicineId: number) => {
  console.log('查看处方二维码，药品ID:', medicineId)
  alert(`查看处方二维码 - 药品ID: ${medicineId}\n\n这里会显示处方的二维码图片，可用于扫码验证。`)
}

// 页面加载时获取数据
onMounted(() => {
  loadOrderDetail()
})
</script>

<style scoped>
.prescription-order-detail {
  width: 100%;
  padding: 20px;
  background-color: #f0f2f5;
  min-height: 100vh;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  background: white;
  padding: 20px 25px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.page-header h1 {
  margin: 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.back-button {
  background-color: #1890ff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.back-button:hover {
  background-color: #40a9ff;
}

.loading {
  text-align: center;
  padding: 50px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #f3f3f3;
  border-top: 4px solid #1890ff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin: 0 auto 20px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.detail-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.info-section {
  background: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.info-section h2 {
  margin: 0 0 20px 0;
  color: #333;
  font-size: 18px;
  font-weight: 600;
  border-bottom: 2px solid #1890ff;
  padding-bottom: 10px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
}

.info-item {
  display: flex;
  align-items: center;
}

.info-item label {
  font-weight: 600;
  color: #666;
  min-width: 120px;
  margin-right: 10px;
  font-size: 14px;
}

.info-item span {
  color: #333;
  font-size: 14px;
}

.info-block {
  margin-bottom: 20px;
}

.info-block label {
  font-weight: 600;
  color: #666;
  display: block;
  margin-bottom: 8px;
}

.info-block p {
  color: #333;
  line-height: 1.6;
  margin: 0;
  padding: 10px;
  background-color: #f8f9fa;
  border-radius: 4px;
  font-size: 14px;
}

.medicine-section {
  margin-top: 20px;
}

.medicine-section h3 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.medicine-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.medicine-item {
  display: flex;
  gap: 15px;
  padding: 15px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #fafafa;
}

.medicine-image {
  width: 80px;
  height: 80px;
  flex-shrink: 0;
}

.medicine-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.medicine-placeholder {
  width: 100%;
  height: 100%;
  background-color: #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 12px;
  border-radius: 4px;
}

.medicine-info {
  flex: 1;
}

.medicine-info h4 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.medicine-spec,
.medicine-price,
.medicine-usage {
  margin: 4px 0;
  color: #666;
  font-size: 14px;
  line-height: 1.4;
}

.medicine-price {
  color: #f5222d;
  font-weight: 600;
}

.prescription-header {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.prescription-no,
.prescription-time,
.prescription-status,
.doctor-info,
.department-info {
  display: flex;
  align-items: center;
}

.prescription-no label,
.prescription-time label,
.prescription-status label,
.doctor-info label,
.department-info label {
  font-weight: 600;
  color: #666;
  margin-right: 8px;
}

.diagnosis-section {
  margin-bottom: 20px;
}

.diagnosis-item {
  display: flex;
  align-items: flex-start;
  margin-bottom: 10px;
}

.diagnosis-item label {
  font-weight: 600;
  color: #666;
  min-width: 100px;
  margin-right: 10px;
}

.prescription-details h3 {
  margin: 0 0 15px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.prescription-medicines {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.prescription-medicine {
  padding: 15px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  background-color: #fafafa;
}

.medicine-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.medicine-header h4 {
  margin: 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
}

.quantity {
  color: #1890ff;
  font-weight: 600;
}

.specification,
.dosage {
  margin: 5px 0;
  color: #666;
  font-size: 14px;
  line-height: 1.4;
}

.action-buttons {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

.view-button {
  background-color: #1890ff;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
}

.view-button:hover {
  background-color: #40a9ff;
}

.order-info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 15px;
}

.amount,
.discount,
.final-amount {
  font-weight: 600;
}

.amount {
  color: #333;
}

.discount {
  color: #52c41a;
}

.final-amount {
  color: #f5222d;
  font-size: 16px;
}

.status-pending {
  color: #fa8c16;
  font-weight: 600;
}

.status-completed {
  color: #52c41a;
  font-weight: 600;
}

.status-invalid {
  color: #f5222d;
  font-weight: 600;
}

.empty-state {
  text-align: center;
  padding: 50px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.empty-state p {
  color: #999;
  font-size: 16px;
  margin: 0;
}

@media (max-width: 768px) {
  .prescription-order-detail {
    padding: 10px;
  }
  
  .page-header {
    flex-direction: column;
    gap: 15px;
    text-align: center;
  }
  
  .info-grid,
  .order-info-grid {
    grid-template-columns: 1fr;
  }
  
  .medicine-item {
    flex-direction: column;
  }
  
  .medicine-image {
    align-self: center;
  }
  
  .action-buttons {
    flex-direction: column;
  }
}
</style> 